package com.jss.lanouhn.fristapp.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.fragments.Fragment_Category;
import com.jss.lanouhn.fristapp.fragments.Fragment_Home;
import com.jss.lanouhn.fristapp.fragments.Fragment_Profile;
import com.jss.lanouhn.fristapp.fragments.Fragment_Selecter;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RadioButton tabHome, tabSelecte, tabCategory, tabMine;

    private FragmentManager manager;

    private FragmentTransaction transaction;

    private Fragment_Home fragment_home;
    private Fragment_Selecter fragment_selecter;
    private Fragment_Category fragment_category;
    private Fragment_Profile fragment_profile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {

        fragment_home = new Fragment_Home();
        fragment_selecter = new Fragment_Selecter();
        fragment_category = new Fragment_Category();
        fragment_profile = new Fragment_Profile();

        tabHome = (RadioButton) findViewById(R.id.rb_home);
        tabSelecte = (RadioButton) findViewById(R.id.rb_hot);
        tabCategory = (RadioButton) findViewById(R.id.rb_classify);
        tabMine = (RadioButton) findViewById(R.id.rb_mine);

        tabHome.setChecked(true);

        manager = getSupportFragmentManager();

        transaction = manager.beginTransaction();




        transaction.add(R.id.fr_contain, fragment_profile, "Tab_profile");
        transaction.add(R.id.fr_contain, fragment_category, "Tab_category");
        transaction.add(R.id.fr_contain, fragment_selecter, "Tab_selecter");
        transaction.add(R.id.fr_contain, fragment_home, "Tab_home");

        transaction.commit();

        tabHome.setOnClickListener(this);
        tabSelecte.setOnClickListener(this);
        tabCategory.setOnClickListener(this);
        tabMine.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {


        manager = getSupportFragmentManager();

        transaction = manager.beginTransaction();

        switch (v.getId()) {
            case R.id.rb_home:
                transaction.show(fragment_home);
               transaction.hide(fragment_category);
                transaction.hide(fragment_selecter);
                transaction.hide(fragment_profile);
                break;
            case R.id.rb_hot:
                transaction.show(fragment_selecter);
                transaction.hide(fragment_home);
                transaction.hide(fragment_category);
                transaction.hide(fragment_profile);
                break;
            case R.id.rb_classify:
                transaction.show(fragment_category);
                transaction.hide(fragment_home);
                transaction.hide(fragment_selecter);
                transaction.hide(fragment_profile);
                break;
            case R.id.rb_mine:
                transaction.show(fragment_profile);
                transaction.hide(fragment_home);
                transaction.hide(fragment_selecter);
                transaction.hide(fragment_category);
                break;
        }
        transaction.commit();
    }

}
